#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

string getPermutation(int n, int k) {
    vector<int> f(n);
    f[0] = 1;
    for (int i = 1; i < n; i++)
        f[i] = f[i - 1] * i;

    k--;
    string res;
    vector<int> valid(n + 1, 1);
    for (int i = 1; i <= n; i++)
    {
        int t = k / f[n - i] + 1;
        for (int j = 1; j <= n; j++)
        {
            t -= valid[j];
            if (!t)
            {
                res += (j + '0');
                valid[j] = 0;
                break;
            }
        }
        k %= f[n - i];
    }
    return res;
}

int main()
{
    getPermutation(4,9);
    return 0;
}